package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.RequestMapping;
import com.nercel.dsj.gksales.model.entity.Permission;
import com.nercel.dsj.gksales.service.PermissionService;
import com.nercel.dsj.gksales.shiro.PermissionType;
import com.nercel.dsj.gksales.shiro.service.ShiroService;
import com.nercel.dsj.gksales.shiro.util.ShiroUtil;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import com.nercel.dsj.gksales.util.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author sunlin
 * @date 2018-11-18
 */
@RestController
@Api(tags = "权限接口")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PermissionApiController {

    private final PermissionService permissionService;

    private final ShiroService shiroService;

    @GetMapping(value = "/api/v1/permissions")
    @ApiOperation(value = "权限列表", response = Permission.class)
    public List<Permission> list(@ApiParam(hidden = true) QueryParams queryParams,
                                 @ApiParam(value = "查询关键词") @RequestParam(required = false) String keyword,
                                 @ApiParam(value = "是否分配给角色") @RequestParam(required = false) Boolean assignable) {
        queryParams.put("keyword", keyword);
        queryParams.put("assignable", assignable);
        return permissionService.list(queryParams);
    }

    @PutMapping(value = "/api/v1/permissions/{id}")
    @ApiOperation(value = "更新权限信息")
    public Permission update(@ApiParam(value = "数据ID") @PathVariable Integer id,
                         @ApiParam(value = "权限信息") @RequestBody Permission permission
    ) {
        permission.setId(id);
        permissionService.update(permission);
        shiroService.reloadFilterChain();
        return permission;
    }

    @GetMapping(value = "/api/v1/permissions/{id}")
    @ApiOperation(value = "数据表信息", response = Permission.class)
    public Object get(@ApiParam(value = "ID") @PathVariable Integer id) {
        Permission permission = permissionService.findById(id);
        return ApiResultUtil.notNull(permission);
    }

    @PostMapping(value = "/api/v1/permissions")
    @ApiOperation(value = "新增权限")
    public Permission create(@ApiParam(value = "权限信息") @RequestBody Permission permission) {
        permissionService.save(permission);
        shiroService.reloadFilterChain();
        return permission;
    }

    @DeleteMapping(value = "/api/v1/permissions/{id}")
    @ApiOperation(value = "删除权限")
    public Object delete(@ApiParam(value = "权限ID") @PathVariable Integer id) {
        permissionService.delete(id);
        shiroService.reloadFilterChain();
        return ApiResultUtil.success();
    }

    @GetMapping(value = "/api/v1/permissions/status")
    @ApiOperation(value = "权限配置验证")
    public Boolean check(@ApiParam(value = "权限配置") @RequestParam(required = false) String perms) {
        return ShiroUtil.checkPermssionsConfig(perms);
    }

    @GetMapping(value = "/api/v1/permissions/filters")
    @ApiOperation(value = "手动刷新SHIRO权限配置")
    public Object update() {
        shiroService.reloadFilterChain();
        return ApiResultUtil.success();
    }

    @GetMapping(value = "/api/v1/permissions/checking/nomappings")
    @ApiOperation(value = "获取未映射过的系统URL")
    public List<RequestMapping> getNoPermissionUrls(HttpServletRequest request) {
        return permissionService.getNoPermissionRequestMapping(SpringUtil.getRequestMapping(request));
    }


    @GetMapping(value = "/api/v1/permissions/add/nomappings")
    @ApiOperation(value = "增加获取未映射过的系统URL")
    public Object insertNoPermissionUrls(HttpServletRequest request) {
        List<RequestMapping> requestMappings = permissionService.getNoPermissionRequestMapping(SpringUtil.getRequestMapping(request));
        double step = 10;
        for(int i=0; i<requestMappings.size(); i++){
            RequestMapping requestMapping = requestMappings.get(i);
            Permission perm = new Permission();
            perm.setAssignable(false);
            perm.setState(PermissionType.API_URL);
            perm.setName(requestMapping.getName() + "API");
            perm.setUrl(requestMapping.getPatternUrl());
            perm.setPermission("anon");
            perm.setOrderIndex(10000D + step * i);
            permissionService.save(perm);
        }
        return ApiResultUtil.success();
    }


}
